package com.jindi.appuser.util;

import com.alibaba.fastjson.JSON;
import com.jindi.appuser.common.EmptyUtil;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Likp
 * @date 2018/7/18 17:25
 * Description:
 */

public class LotUtil {

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("(-?[0-9]+.?[0-9]+)|(-?[0-9])");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * 将object转成json返回给客户端
     * @param obj
     */
    public static void outJsonToClient(Object obj) {
        PrintWriter pw = null;
        pw.print(JSON.toJSON(obj).toString());
        // resp.getWriter().print(JSON.toJSON(maps).toString());

    }


    /**
     * String转换成int，出异常时返回 -1
     * @param str
     * @return
     */
    public static int StringToIntException(String str) {
        int rtn = 0;
        try {
            DecimalFormat df = new DecimalFormat("#0");
            Double db = new Double(str);
            rtn = Integer.parseInt(df.format(db));
        }catch(Exception e) {
            return -1;
        }
        return rtn;
    }

    /**
     * 根据给定字符长度随机生成字符串
     * @param length
     * @return
     */
    public static String createRandomString(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random rd = new Random();
        StringBuffer sb =new StringBuffer();
        for(int i = 0; i < length; i++) {
            sb.append(base.charAt(rd.nextInt(base.length())));
        }
        return sb.toString();
    }

    public static long countDown(String nextTime, String lotteryName){
        long ad = 0;
        if (!EmptyUtil.isEmpty(nextTime)){
            Calendar calendar = new GregorianCalendar(Integer.parseInt(nextTime.subSequence(0,4).toString()),
                    Integer.parseInt(nextTime.subSequence(4,6).toString())-1,
                    Integer.parseInt(nextTime.subSequence(6,8).toString()),
                    Integer.parseInt(nextTime.subSequence(8,10).toString()),
                    Integer.parseInt(nextTime.subSequence(10,12).toString()),
                    Integer.parseInt(nextTime.subSequence(12,14).toString()));
            Calendar cal = Calendar.getInstance();
            int sc = 35;
            if(lotteryName.contains("ffc")){
                sc = 3;
            }
            ad = calendar.getTimeInMillis()/1000-cal.getTimeInMillis()/1000+sc;
        }
        return ad < 0 ? 0 : ad;
    }
}
